package com.example.recordingpen_datacollection.controller;


import com.example.recordingpen_datacollection.entity.Query.LndividualOrderQue;
import com.example.recordingpen_datacollection.entity.Query.RequestAnOrderQue;
import com.example.recordingpen_datacollection.entity.Requestanorder;
import com.example.recordingpen_datacollection.entity.Vo.RequestanorderCertaintyVo;
import com.example.recordingpen_datacollection.entity.Vo.RequestanorderVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.RequestanorderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-06-07
 */
@Api(tags = "订单信息")
@RestController
@RequestMapping("/requestanorder")
public class RequestanorderController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private RequestanorderService requestanorderService;


    @ApiOperation(value = "提交订单Uesr [user]")
    @PostMapping("setRequestanorder")
    public RLIST setRequestanorder(@RequestBody RequestanorderVo requestanorderVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 51 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        boolean flat = requestanorderService.setRequestanorder(requestanorderVo,value);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @ApiOperation(value = "确定订单admin [admin]")
    @PostMapping("requestanorderCertainty")
    public RLIST requestanorderCertainty(@RequestBody RequestanorderCertaintyVo requestanorderCertaintyVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        Requestanorder byId = requestanorderService.getById(requestanorderCertaintyVo.getRid());
        if(byId == null){
            return RLIST.error().message("订单不存在");
        }
        if(byId.getOrderStatus() == 1){
            return RLIST.error().message("订单已被激活");
        }
        boolean flat = requestanorderService.alterRequestanorderCertainty(byId);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }
    @ApiOperation(value = "查询订单admin [admin]")
    @PostMapping("getRequestAnOrder")
    public RLIST getRequestAnOrder(@RequestBody RequestAnOrderQue requestAnOrderQue, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        RLIST rlist = requestanorderService.getRequestAnOrderPage(requestAnOrderQue);
        return rlist;
    }

    @ApiOperation(value = "查询自己的订单 [user]")
    @PostMapping("getPersonnelOrders")
    public RLIST getPersonnelOrders(@RequestBody LndividualOrderQue lndividualOrderQue, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 51 ){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);
        RLIST rlist = requestanorderService.getPersonnelOrdersPage(lndividualOrderQue,value);
        return rlist;
    }
}

